package net.bsayiner.sadeceHaber.Activities;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import net.bsayiner.sadeceHaber.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

public class NewsInfoActivity extends Activity {

	private String newsInfo = "";
	private ProgressDialog progressDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Ekran başlığını gizler
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.news_info);
		initializeComponents();
	}

	private void initializeComponents() {
		TextView txtCategory = (TextView) findViewById(R.id.txtCategory);
		TextView txtTitle = (TextView) findViewById(R.id.txtTitle);
		TextView txtPubDate = (TextView) findViewById(R.id.txtPubDate);
		ImageView imgNewsImage = (ImageView) findViewById(R.id.imgNews);
		TextView txtDescription = (TextView) findViewById(R.id.txtDescription);
		String category = getIntent().getExtras().getString("category");
		String title = getIntent().getExtras().getString("title");
		String pubDate = getIntent().getExtras().getString("pubDate");
		String newsImage = getIntent().getExtras().getString("imageURL");
		String description = getIntent().getExtras().getString("description");
		final String link = getIntent().getExtras().getString("newsLink");
		txtCategory.setText(category);
		txtTitle.setText(title);
		txtPubDate.setText(pubDate);
		txtDescription.setText(description);
		BitmapManager.INSTANCE.loadBitmap(newsImage, imgNewsImage);
		txtDescription.setText(description);
		if (link.contains("www")) {
			progressDialog = ProgressDialog.show(NewsInfoActivity.this, "", "Haber bilgisi güncelleniyor...");
			new Thread(new Runnable() {
				public void run() {
					Document document = null;
					Element news;
					try {
						System.err.println(link);
						document = Jsoup.connect(link).get();
						news = document.select("div.haberMetni").first();
						newsInfo = Jsoup.parse(news.toString()).text();
					} catch (IOException e) {
						e.printStackTrace();
					} finally {
						handler.sendEmptyMessage(0);
					}
				}
			}).start();
		}
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			TextView txtMoreNews = (TextView) findViewById(R.id.txtMoreNews);
			txtMoreNews.setText(newsInfo);
			progressDialog.dismiss();
		}
	};

}
